package com.example.myapplication.movie;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.ViewapteTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.MoviecarouselmapBean;
import com.example.myapplication.ShuJu.MsvieinformationlistBean;
import com.example.myapplication.apter.MsvieinformationlistApter;

import org.w3c.dom.Text;

import java.util.ArrayList;

import okhttp3.OkHttpClient;

public class MovieActivity extends AppCompatActivity {

    private ImageView but_lawyerList;
    private ViewPager vp;
    private RecyclerView review;
    private TextView showmore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie);
        initview();
        // 返回
        but_bloak();
        //轮播图
        carouselmap();
        //影片信息列表
        movieinformationlist();

    }

    private void showmorelist(MsvieinformationlistBean msvieinformationlistBean) {
        showmore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mores(msvieinformationlistBean,msvieinformationlistBean.getRows().size());
                showmore.setVisibility(View.INVISIBLE);
            }
        });
    }

    private void movieinformationlist() {
        OKHttpTool.doNetRequest("prod-api/api/movie/film/list",
                OKHttpTool.GET,
                MsvieinformationlistBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                MsvieinformationlistBean msvieinformationlistBean = (MsvieinformationlistBean)obj;
                                if(msvieinformationlistBean.getCode()==200){
                                    mores(msvieinformationlistBean,5);
                                    //显示更多
                                    showmorelist(msvieinformationlistBean);
                                }

                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    public void  mores(MsvieinformationlistBean msvieinformationlistBean, Integer nums){
        review.setAdapter(new MsvieinformationlistApter(MovieActivity.this,msvieinformationlistBean.getRows(),nums));
        review.setLayoutManager(new LinearLayoutManager(MovieActivity.this));
    }

    private void carouselmap() {
        OKHttpTool.doNetRequest("prod-api/api/movie/rotation/list",
                OKHttpTool.GET,
                MoviecarouselmapBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                MoviecarouselmapBean moviecarouselmapBean =(MoviecarouselmapBean)obj;
                                if(moviecarouselmapBean.getCode()==200){
                                    ArrayList<String> list = new ArrayList<>();
                                    for (MoviecarouselmapBean.RowsBean row : moviecarouselmapBean.getRows()) {
                                        list.add(row.getAdvImg());
                                    }
                                    ViewapteTool viewapteTool = new ViewapteTool(MovieActivity.this,list);
                                    vp.setAdapter(viewapteTool);
                                    mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
                                }


                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void but_bloak() {
        but_lawyerList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MovieActivity.this.finish();
            }
        });
    }

    private void initview() {
        but_lawyerList = findViewById(R.id.but_lawyerList);
        vp = findViewById(R.id.vp);
        review = findViewById(R.id.review);
        showmore = findViewById(R.id.Showmore);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                int index = vp.getCurrentItem();
                index = (index + 1) % 3;
                vp.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 *1000);
            }
        }
    };
}